home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / vdigit.zip / VRTEST.C < prev    next >
C/C++ Source or Header  |  1989-06-24  |  4KB  |  169 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*            Digitized Voice Programmer's Toolkit                */
  4. /*            ------------------------------------                */
  5. /*                                                                */
  6. /*        Example of a program which records voice data           */
  7. /*               to a file of unlimited length                    */
  8. /*                                                                */
  9. /*           Copyright (c) 1989, Farpoint Software                */
  10. /*                                                                */
  11. /*                                                                */
  12. /*  This program simply records a voice message while displaying  */
  13. /*  a message repeatedly indicating the count of bytes recorded.  */
  14. /*  The digitized voice data is written a disk file piece by      */
  15. /*  piece while the recording is in progress. The memory buffer   */
  16. /*  used is much smaller than the potential file size. Data is    */
  17. /*  written to the buffer with the pointer wrapping back to the   */
  18. /*  beginning of the buffer upon reaching the end. The routine    */
  19. /*  RVOICE_CATCHUP (source in VRMOD.ASM) transfers data from the  */
  20. /*  buffer to the file in the same way.                           */
  21. /*                                                                */
  22. /******************************************************************/
  23.  
  24. #include "vrmod.h"
  25.  
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <malloc.h>
  29. #include <fcntl.h>
  30. #include <io.h>
  31. #include <sys\types.h>
  32. #include <sys\stat.h>
  33. #include <bios.h>
  34. #include <string.h>
  35.  
  36. #define buf_size  16384L
  37.  
  38. char filename[128];
  39. char huge *buffer;
  40. int handle;
  41. short comport;
  42.  
  43. main(argc,argv)
  44. int argc;
  45. char ** argv;
  46.  
  47. {
  48. short retval;
  49. long count,index;
  50.  
  51. /* make sure there are two arguments on the command line */
  52.  
  53. if (argc != 3)
  54.     {
  55.     printf("Command line must include COM port number (1 - 4) and file name.\n");
  56.     exit(1);
  57.     }
  58.  
  59. /* get the COM port number */
  60.  
  61. argv++;
  62. comport = (short)((**argv) - '0');
  63.  
  64. /* get the file name */
  65.  
  66. argv++;
  67. strcpy(filename,*argv);
  68.  
  69. /* open or create the file, truncating if it already exists */
  70.  
  71. handle = open(filename,O_BINARY|O_RDWR|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);
  72. if (handle == -1)
  73.     {
  74.     printf("Error opening file.\n");
  75.     exit(1);
  76.     }
  77.  
  78. /* allocate memory for the buffer */
  79.  
  80. buffer = (char huge *)halloc(buf_size,1);
  81. if (buffer == NULL)
  82.     {
  83.     close(handle);
  84.     printf("Unable to allocate buffer memory.\n");
  85.     exit(1);
  86.     }
  87.  
  88. /* get ready */
  89.  
  90. RVOICE_INIT();
  91.  
  92. /* start the process */
  93.  
  94. retval = RVOICE_START(buffer,(long)buf_size,1,handle,comport,0L);
  95.  
  96. /* check the return code for errors */
  97.  
  98. switch (retval)
  99.     {
  100.     case 0:
  101.         printf("Voice recording has begun.\n");
  102.         break;
  103.     case 1:
  104.         printf("Buffer sizing error.\n");
  105.         break;
  106.     case 3:
  107.         printf("Invalid COM port.\n");
  108.     }
  109.  
  110. if (!retval)
  111.     {
  112.  
  113.     /* loop while testing for the <Esc> key which ends recording */
  114.  
  115.     while (1)
  116.         {
  117.  
  118.         /* RVOICE_CATCHUP must be called frequently to keep writing */
  119.         /* the data to the disk as it becomes available without     */
  120.         /* allowing buffer wrap-around to catch up with us.         */
  121.         /* If RVOICE_CATCHUP returns 2 then file write error. */
  122.  
  123.         if (RVOICE_CATCHUP() == 2)
  124.             {
  125.             printf("File write error.\n");
  126.             break;
  127.             }
  128.  
  129.         /* doodle in the foreground while all this is happening */
  130.  
  131.         RVOICE_STATUS(&count,&index);
  132.         printf("Count of bytes recorded = %ld\n",count);
  133.  
  134.         /* check the keystroke buffer */
  135.  
  136.         if (_bios_keybrd(_KEYBRD_READY))
  137.             {
  138.  
  139.             /* key pressed, test for <Esc> key */
  140.  
  141.             if ( (_bios_keybrd(_KEYBRD_READ) >> 8) == 0x01)
  142.                 {
  143.                 RVOICE_STOP();
  144.                 printf("Voice recording ended.\n");
  145.                 break;
  146.                 }
  147.             }
  148.         }
  149.     }
  150.  
  151. if (!retval)
  152.     {
  153.  
  154.     /* we need to make sure it is all written */
  155.  
  156.     while (RVOICE_CATCHUP() == 0);
  157.     }
  158.  
  159. /* call RVOICE_CLEANUP before exiting or crash later */
  160.  
  161. RVOICE_CLEANUP();
  162.  
  163. /* close the file and free the allocated memory */
  164.  
  165. close(handle);
  166. free(buffer);
  167.  
  168. }
  169.